home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / IDXTEXT.ICN < prev    next >
Text File  |  1992-09-28  |  4KB  |  128 lines

  1. ############################################################################
  2. #
  3. #    File:     idxtext.icn
  4. #
  5. #    Subject:  Program for creating indexed text-base
  6. #
  7. #    Author:   Richard L. Goerwitz
  8. #
  9. #    Date:     July 9, 1991
  10. #
  11. ###########################################################################
  12. #
  13. #    Version:  1.12
  14. #
  15. ###########################################################################
  16. #
  17. #      idxtext turns a file associated with gettext() routine into an
  18. #  indexed text-base.  Though gettext() will work fine with files
  19. #  that haven't been indexed via idxtext(), access is faster if the
  20. #  indexing is done if the file is, say, over 10k (on my system the
  21. #  crossover point is actually about 5k).
  22. #
  23. #      Usage is simply "idxtext [-a] file1 [file2 [...]]," where file1,
  24. #  file2, etc are the names of gettext-format files that are to be
  25. #  (re-)indexed.  The -a flag tells idxtext to abort if an index file
  26. #  already exists.
  27. #
  28. #      Indexed files have a very simple format: keyname tab offset
  29. #  [tab offset [etc.]]\n.  The first line of the index file is a
  30. #  pointer to the last indexed byte of the text-base file it indexes.
  31. #
  32. #  BUGS: Index files are too large.  Also, I've yet to find a portable
  33. #  way of creating unique index names that are capable of being
  34. #  uniquely identified with their original text file.  It might be
  35. #  sensible to hard code the name into the index.  The chances of a
  36. #  conflict seem remote enough that I haven't bothered.  If you're
  37. #  worried, use the -a flag.
  38. #
  39. ############################################################################
  40. #
  41. #  Links: adjuncts
  42. #
  43. #  Requires: UNIX or MS-DOS
  44. #
  45. #  See also: gettext.icn
  46. #
  47. ############################################################################
  48.  
  49.  
  50. # declared in adjuncts.icn
  51. # global _slash, _baselen
  52.  
  53. procedure main(a)
  54.     local ABORT, idxfile_name, fname, infile, outfile, _slash, _baselen
  55.     local Pathname, getidxname
  56.  
  57.     initial {
  58.     if find("UNIX"|"Amiga", &features) then {
  59.         _slash := "/"
  60.         _baselen := 10
  61.     }
  62.     else if find("MS-DOS", &features) then {
  63.         _slash := "\\"
  64.         _baselen := 8
  65.     }
  66.     else stop("idxtext:  OS not supported")
  67.     }
  68.  
  69.     if \a[1] == "-a" then ABORT := pop(a)    
  70.  
  71.     # Check to see if we have any arguments.
  72.     *a = 0 & stop("usage: idxtext [-a] file1 [file2 [...]]")
  73.  
  74.     # Start popping filenames off of the argument list.
  75.     while fname := pop(a) do {
  76.  
  77.     # Open input file.
  78.     infile := open(fname) |
  79.         { write(&errout, "idxtext:  ",fname," not found"); next }
  80.     # Get index file name.
  81.     idxfile_name := Pathname(fname) || getidxname(fname)
  82.     if \ABORT then if close(open(idxfile_name)) then
  83.         stop("idxtext:  index file ",idxfile_name, " already exists")
  84.     outfile := open(idxfile_name, "w") |
  85.         stop("idxtext:  can't open ", idxfile_name)
  86.  
  87.     # Write index to index.IDX file.
  88.     write_index(infile, outfile)
  89.  
  90.     every close(infile | outfile)
  91.  
  92.     }
  93.  
  94. end
  95.  
  96.  
  97. procedure write_index(in, out)
  98.  
  99.     local key_offset_table, w, line, KEY
  100.  
  101.     # Write to out all keys in file "in," with their byte
  102.     # offsets.
  103.  
  104.     key_offset_table := table()
  105.  
  106.     while (w := where(in), line := read(in)) do {
  107.     line ? {
  108.         if ="::" then {
  109.         KEY := trim(tab(0))
  110.         if not (/key_offset_table[KEY] := KEY || "\t" || w)
  111.         then stop("idxtext:  duplicate key, ",KEY)
  112.         }
  113.     }
  114.     }
  115.  
  116.     # First line of index contains the offset of the last
  117.     # indexed byte in write_index, so that we can still
  118.     # search unindexed parts of in.
  119.     write(out, where(in))
  120.  
  121.     # Write sorted KEY\toffset lines.
  122.     if *key_offset_table > 0 then
  123.     every write(out, (!sort(key_offset_table))[2])
  124.  
  125.     return
  126.  
  127. end
  128.